package com.chance.cc.crawler.development.scripts.iask;

import com.alibaba.fastjson.JSONObject;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerRecordContext;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterInfo;
import com.chance.cc.crawler.core.pipeline.result.CrawlerData;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.core.script.crawler.CrawlerCommonScript;
import com.chance.cc.crawler.core.selector.Json;
import com.chance.cc.crawler.core.selector.Selectable;
import com.chance.cc.crawler.core.tags.LabelTag;
import com.chance.cc.crawler.core.tags.crawler.CrawlerBusinessTags;
import com.chance.cc.crawler.core.tags.crawler.CrawlerResultTags;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerDataType.*;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.*;
import static com.chance.cc.crawler.development.scripts.allfeild.AutoVMCommonField.Tag_Field_Topic_Type;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/4/9 16:38
 * @Description 爱问问答
 **/
public class IASKCrawlerScript extends CrawlerCommonScript {

    private static Logger log = LoggerFactory.getLogger(IASKCrawlerScript.class);
    private static final String DOMAIN = "iask";
    private static final String SITE = "searchKw";
    private static final String REQUEST_AGAIN_TAG = DOMAIN + "_request_again";
    private static final String SEARCH_KW_SOURCE_URL = "searchKwSourceUrl";

    private static final String PREFIX = "https://iask.sina.com.cn";
    private static final String ENTRANCE_URL = "https://iask.sina.com.cn/search";
    private static final String TURN_PAGE_URL = "https://iask.sina.com.cn/search\\?searchWord=\\S*&page=\\d+";
    private static final String ITEM_URL = "http[s]*://iask.sina.com.cn/b/[0-9a-zA-Z]+.html";
    private static final String COMMENT_URL = "http[s]*://iask.sina.com.cn/b/[0-9a-zA-Z]+.html#comment";

    /**
     * 脚本domain定义
     *
     * @return
     */
    @Override
    public String domain() {
        return DOMAIN;
    }

    /**
     * 进入脚本的正则列表
     */
    @Override
    public void initUrlRegulars() {
        addUrlRegular(ENTRANCE_URL);
        addUrlRegular(TURN_PAGE_URL);
        addUrlRegular(ITEM_URL);
        addUrlRegular(COMMENT_URL);
    }

    /**
     * 是否执行脚本 ： 输入数据检查，合格的才进入脚本
     *
     * @param crawlerRequestRecord
     * @return
     */
    @Override
    public boolean crawlerCheck(CrawlerRequestRecord crawlerRequestRecord) {
        String siteTag = crawlerRequestRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");
        return siteTag.equals(SITE);
    }

    @Override
    public List<CrawlerRecord> prepareRequest(CrawlerRequestRecord requestRecord, List<CrawlerRequestRecord> supportSourceRecords) {
        List<CrawlerRecord> crawlerRecords = new ArrayList<>();
        for (CrawlerRequestRecord supportSourceRecord : supportSourceRecords) {
            String url = supportSourceRecord.getHttpRequest().getUrl();
            if (url.contains("keys")) {
                initKeyword(requestRecord, supportSourceRecord, crawlerRecords);
            }
        }
        return crawlerRecords;
    }

    /**
     * 解析链接方法
     *
     * @param crawlerRequestRecord
     * @param httpPage
     * @return
     */
    @Override
    public List<CrawlerRequestRecord> parseLinks(CrawlerRequestRecord crawlerRequestRecord, HttpPage httpPage) {
        List<CrawlerRequestRecord> parsedLinks = new ArrayList<CrawlerRequestRecord>();
        String requestUrl = crawlerRequestRecord.getHttpRequest().getUrl();

        if (!httpPage.isDownloadSuccess() || (httpPage.getStatusCode() != 200 && httpPage.getStatusCode() != 404)) {
            log.error("{} status code : [{}],will retry!",requestUrl,httpPage.getStatusCode());
            requestAgainCrawlerRecord(parsedLinks, crawlerRequestRecord);
            crawlerRequestRecord.setNeedWashPage(false);
            return parsedLinks;
        }

        if(httpPage.getStatusCode() == 404){
            log.error("{} status code : [{}]",requestUrl,httpPage.getStatusCode());
            crawlerRequestRecord.setNeedWashPage(false);
            return parsedLinks;
        }

        if (requestUrl.matches(TURN_PAGE_URL)) {
            turnPageUrlRecord(crawlerRequestRecord, httpPage, parsedLinks);
        }

        if (requestUrl.matches(ITEM_URL)) {
            itemUrlRecord(crawlerRequestRecord, httpPage, parsedLinks);
        }
        return parsedLinks;
    }

    private void turnPageUrlRecord(CrawlerRequestRecord crawlerRequestRecord, HttpPage httpPage, List<CrawlerRequestRecord> parsedLinks) {
        String requestUrl = httpPage.getRequest().getUrl();
        //翻页
        String isTurn = httpPage.getHtml().xpath("//a[text()='下一页']/text()").get();
        if (StringUtils.isNotBlank(isTurn)) {
            String turnUrl = getTurnUrl(requestUrl, "page", "searchWord");
            CrawlerRequestRecord turnRecord = CrawlerRequestRecord.builder()
                    .turnPageRequest(crawlerRequestRecord)
                    .httpUrl(turnUrl)
                    .releaseTime(System.currentTimeMillis())
                    .httpHead("referer", requestUrl)
                    .copyBizTags()
                    .copyResultTags()
                    .build();
            parsedLinks.add(turnRecord);
        }

        //列表解析
        List<Selectable> nodes = httpPage.getHtml().xpath("//ul[@class=\"iask-search-list\"]/li").nodes();
        for (Selectable node : nodes) {
            String itemUrl = node.xpath("./p[@class=\"title-text\"]/a/@href").get();
            if (StringUtils.isBlank(itemUrl)) {
                continue;
            } else {
                itemUrl = PREFIX + itemUrl;
            }

            String comments = node.xpath(".//span[@class=\"num-text\"]/em/text()").get();
            if (StringUtils.isBlank(comments)) {
                System.out.println(itemUrl);
            }
            if (Integer.parseInt(comments.trim()) < 1) {
                continue;
            }

            CrawlerRequestRecord itemRecord = CrawlerRequestRecord.builder()
                    .itemPageRequest(crawlerRequestRecord)
                    .httpUrl(itemUrl)
                    .releaseTime(System.currentTimeMillis())
                    .copyBizTags()
                    .needWashed(false)
                    .copyResultTags()
                    .build();
            itemRecord.getHttpRequest().addExtra("comments", comments);
            parsedLinks.add(itemRecord);
        }
    }

    private void itemUrlRecord(CrawlerRequestRecord crawlerRequestRecord, HttpPage httpPage, List<CrawlerRequestRecord> parsedLinks) {
        //进行时间范围的判断
        String releaseTime = httpPage.getHtml().xpath("//span[@class=\"ask-time mr10\"]/text()").get();
        Long date = null;
        try {
            if(StringUtils.isBlank(releaseTime)){
                List<String> all = httpPage.getHtml().xpath("//ul[@class=\"detail-answer-list\"]/li//p[@class=\"time\"]/text()|//ul[@class=\"new-answer-list\"]/li//p[@class=\"time\"]/text()").all();
                if(all==null || all.size() < 1){
                    requestAgainCrawlerRecord(parsedLinks,crawlerRequestRecord);
                    return;
                }
                date = DateUtils.parseDate(all.get(0).trim(), "yyyy-MM-dd HH:mm:ss").getTime();
                for (String time : all) {
                    if(StringUtils.isBlank(time)){
                        continue;
                    }
                    Long currentDate = DateUtils.parseDate(time.trim(), "yyyy-MM-dd HH:mm:ss").getTime();
                    if(currentDate < date){
                        date = currentDate;
                    }
                }
            }else{
                date =DateUtils.parseDate(releaseTime.trim(), "yyyy-MM-dd HH:mm:ss").getTime();
            }
        } catch (ParseException e) {
            log.error(e.getMessage());
        }
        if(date == null){
            log.error("date is null");
            crawlerRequestRecord.setNeedWashPage(false);
            return;
        }
        crawlerRequestRecord.setReleaseTime(date);
        CrawlerEnum.CrawlerRecordFilter filter = crawlerRequestRecord.getFilter();
        if (filter == CrawlerEnum.CrawlerRecordFilter.keyOrDateRange || filter == CrawlerEnum.CrawlerRecordFilter.dateRange) {
            List<FilterInfo> filterInfos = crawlerRequestRecord.getFilterInfos();
            Long startTime = null;
            Long endTime = null;
            for (FilterInfo filterInfo : filterInfos) {
                if (filterInfo.getFilter() == CrawlerEnum.CrawlerRecordFilter.dateRange) {
                    long[] dateAllowRange = filterInfo.getDateAllowRange();
                    int hourFromNow = filterInfo.getHourFromNow();
                    if (dateAllowRange != null) {
                        startTime = dateAllowRange[0];
                        endTime = dateAllowRange[1];
                    } else if (hourFromNow != 0) {
                        endTime = System.currentTimeMillis();
                        startTime = endTime - DateUtils.MILLIS_PER_HOUR * hourFromNow;
                    }
                }
            }
            if(startTime != null && startTime <= date && date <= endTime){
                crawlerRequestRecord.setNeedWashPage(true);
            }
        } else {
            crawlerRequestRecord.setNeedWashPage(true);
        }

        boolean needWashPage = crawlerRequestRecord.isNeedWashPage();
        if (!needWashPage) {
            return;
        }

        //评论
        CrawlerResultTags resultTags = crawlerRequestRecord.tagsCreator().resultTags();
        if (resultTags.hasDataType(comment)) {
            resultTags.getCategoryTag().removeLabelTag("comment");

            CrawlerRequestRecord filterRecord = resultTags.getCategoryTag().getKVTagObjVal("comment_filter_record", CrawlerRequestRecord.class);
            if (filterRecord == null) {
                log.error("filter record can not null !");
                return;
            }

            String url = httpPage.getRequest().getUrl() + "#comment";
            CrawlerRequestRecord commentRecord = CrawlerRequestRecord.builder()
                    .turnPageRequest(crawlerRequestRecord)
                    .httpUrl(url)
                    .releaseTime(System.currentTimeMillis())
                    .notFilterRecord()
                    .copyBizTags()
                    .needWashed(true)
                    .resultLabelTag(comment)
                    .build();
            commentRecord.setFilter(filterRecord.getFilter());
            commentRecord.setFilterInfos(filterRecord.getFilterInfos());
            commentRecord.getHttpRequest().addExtra("articleUrl",crawlerRequestRecord.getHttpRequest().getUrl());
            parsedLinks.add(commentRecord);
        }
    }

    private String getTurnUrl(String requestUrl, String page, String keyword) {
        String[] split = requestUrl.split("\\?");
        String nextUrl = split[0] + "?";
        List<NameValuePair> parse = URLEncodedUtils.parse(split[1], Charset.defaultCharset());
        for (NameValuePair nameValuePair : parse) {
            String name = nameValuePair.getName();
            String value = nameValuePair.getValue();
            if (StringUtils.isNotBlank(page) && name.equals(page)) {
                nextUrl = nextUrl + name + "=" + (Integer.parseInt(value) + 1) + "&";
            } else if (StringUtils.isNotBlank(keyword) && name.equals(keyword)) {
                try {
                    nextUrl = nextUrl + name + "=" + URLEncoder.encode(value, "UTF-8") + "&";
                } catch (UnsupportedEncodingException e) {
                    log.error(e.getMessage());
                }
            } else {
                nextUrl = nextUrl + name + "=" + value + "&";
            }
        }

        return nextUrl.substring(0, nextUrl.length() - 1);
    }

    @Override
    public List<CrawlerData> washPage(CrawlerRequestRecord crawlerRecord, HttpPage page) {
        List<CrawlerData> crawlerDataList = new ArrayList<>();
        CrawlerResultTags crawlerResultTags = crawlerRecord.tagsCreator().resultTags();
        String url = page.getRequest().getUrl();

        if (crawlerResultTags.hasDataType(article)) {
            crawlerDataList.add(washArticle(crawlerRecord, page));
        }

        if (crawlerResultTags.hasDataType(interaction)) {
            crawlerDataList.add(washInteraction(crawlerRecord, page));
        }

        if (crawlerResultTags.hasDataType(comment)) {
            crawlerDataList.addAll(washComment(crawlerRecord, page));
        }

        return crawlerDataList;
    }

    private CrawlerData washArticle(CrawlerRequestRecord crawlerRequestRecord, HttpPage httpPage) {
        String site = crawlerRequestRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");
        String itemUrl = httpPage.getRequest().getUrl();
        String articleKey = itemUrl.substring(itemUrl.lastIndexOf("/") + 1, itemUrl.lastIndexOf("."));

        String title = httpPage.getHtml().xpath("//p[@class=\"problem-title-text\"]/text()|//div[@class=\"title-f22\"]/pre|//p[@class=\"title-text\"]/text()|//span[@class=\"title-f22\"]").get();
        title = StringUtils.isEmpty(title) ? httpPage.getHtml().xpath("//h1/text()").get() : title;
        String author = httpPage.getHtml().xpath("//span[@class=\"user-name\"]/text()").get();
        author = StringUtils.isEmpty(author) ? httpPage.getHtml().xpath("//span/a").get() :author;
        List<String> all = httpPage.getHtml().xpath("//pre[@class=\"problem-text\"]//text()|//pre[@class=\"question-text\"]//text()|//div[@class=\"question_text\"]//text()").all();
        StringBuffer contentBf = new StringBuffer();
        for (String data : all) {
            contentBf.append(data).append(" ");
        }
        List<String> all1 = httpPage.getHtml().xpath("//div[@class=\"label-list\"]/a/text()").all();
        if(all1 != null && all1.size() > 0){
            crawlerRequestRecord.tagsCreator().bizTags().addCustomKV(Tag_Field_Topic_Type,all1);
        }

        CrawlerData crawlerData = CrawlerData.builder()
                .data(crawlerRequestRecord, httpPage)
                .dataId(StringUtils.joinWith("-", crawlerRequestRecord.getDomain(), site, CrawlerEnum.CrawlerDataType.article.enumVal(), articleKey))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.valueOf("article"))
                .url(itemUrl)
                .releaseTime(crawlerRequestRecord.getReleaseTime())
                .addContentKV(Field_Content, contentBf.toString())
                .addContentKV(Field_Title, title)
                .addContentKV(Field_Author, author)
                .build();
        return crawlerData;
    }

    private CrawlerData washInteraction(CrawlerRequestRecord crawlerRequestRecord, HttpPage httpPage) {
        String site = crawlerRequestRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");
        String itemUrl = httpPage.getRequest().getUrl();
        String articleKey = itemUrl.substring(itemUrl.lastIndexOf("/") + 1, itemUrl.lastIndexOf("."));

        Map<String, Object> extras = httpPage.getRequest().getExtras();
        String comments = (String) extras.get("comments");

        CrawlerData crawlerData = CrawlerData.builder()
                .data(crawlerRequestRecord, httpPage)
                .parentId(StringUtils.joinWith("-", crawlerRequestRecord.getDomain(), site, CrawlerEnum.CrawlerDataType.article.enumVal(), articleKey))
                .dataId(StringUtils.joinWith("-", crawlerRequestRecord.getDomain(), site, interaction.enumVal(), articleKey))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.valueOf("interaction"))
                .url(itemUrl)
                .releaseTime(crawlerRequestRecord.getReleaseTime())
                .addContentKV(Field_I_Comments, comments)
                .build();

        return crawlerData;
    }

    private List<CrawlerData> washComment(CrawlerRequestRecord crawlerRequestRecord, HttpPage httpPage) {
        String site = crawlerRequestRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");
        List<CrawlerData> crawlerDataList = new ArrayList<>();
        String requestUrl = httpPage.getRequest().getUrl();
        String articleUrl = (String) httpPage.getRequest().getExtras().get("articleUrl");
        String articleKey = articleUrl.substring(articleUrl.lastIndexOf("/") + 1, articleUrl.lastIndexOf("."));

        List<Selectable> nodes = httpPage.getHtml().xpath("//ul[@class=\"detail-answer-list\"]/li|//div[@class=\"answer_list\"]/ul/li|//div[@class=\"good_content\"]/div").nodes();
        for (Selectable node : nodes) {
            String commentId = node.xpath(".//ul[@class=\"new-replay-list\"]/@answerid|.//ul[@class=\"iask-comment-list\"]/@answerid").get();
            String author = node.xpath(".//p[@class=\"user-name\"]/a/text()|.//span[@class=\"user_wrap\"]/a/text()|.//div[@class=\"answer_tip\"]/a/text()").get();
            String releaseTime = node.xpath(".//p[@class=\"time\"]/text()|.//span[@class=\"answer_t\"]/text()").get();
            if(StringUtils.isBlank(releaseTime)){
                releaseTime = node.xpath(".//span[@class=\"time mr10\"]/text()").get();
            }
            if(releaseTime.contains("|")){
                releaseTime = releaseTime.split("\\|")[1].trim();
            }
            List<String> all = node.xpath(".//pre[@class=\"list-text\"]//text()|.//div[@class=\"answer_text\"]//text()|.//p[@class=\"list-text\"]/text()").all();
            StringBuffer contentBf = new StringBuffer();
            for (String data : all) {
                contentBf.append(data).append(" ");
            }

            String likes = node.xpath(".//a[@class=\"operation-ele btn-goods praise\"]/span/text()|.//span[@class=\"praise mr15 btn-goods-ok\"]/span/text()|.//span[@class=\"praise mr15\"]/span/text()").get();
            likes = StringUtils.isEmpty(likes) ? "0" : likes;
            String noLikes = node.xpath(".//a[@class=\"operation-ele btn-bad step\"]/span/text()|.//span[@class=\"praise1 mr15 step\"]/span/text()").get();
            noLikes = StringUtils.isBlank(noLikes)? "0" : noLikes;

            try {
                long releaseTimeToLong = DateUtils.parseDate(releaseTime.trim(), "yyyy-MM-dd HH:mm:ss").getTime();
                CrawlerData crawlerData = CrawlerData.builder()
                        .data(crawlerRequestRecord, httpPage)
                        .parentId(StringUtils.joinWith("-", crawlerRequestRecord.getDomain(), site,CrawlerEnum.CrawlerDataType.article.enumVal(), articleKey))
                        .dataId(StringUtils.joinWith("-", crawlerRequestRecord.getDomain(), site,comment.enumVal(), commentId))
                        .resultLabelTag(CrawlerEnum.CrawlerDataType.valueOf("comment"))
                        .releaseTime(releaseTimeToLong)
                        .url(requestUrl)
                        .requestLabelTag(CrawlerEnum.CrawlerRequestType.filter)
                        .requestLabelTag(CrawlerEnum.CrawlerRequestType.result)
                        .addContentKV(Field_Author, author)
                        .addContentKV(Field_Content, contentBf.toString())
                        .build();
                crawlerDataList.add(crawlerData);


                CrawlerData crawlerDataInteraction = CrawlerData.builder()
                        .data(crawlerRequestRecord, httpPage)
                        .parentId(StringUtils.joinWith("-", crawlerRequestRecord.getDomain(), site,comment.enumVal(), commentId))
                        .dataId(StringUtils.joinWith("-", crawlerRequestRecord.getDomain(), site,interaction.enumVal(), commentId))
                        .resultLabelTag(CrawlerEnum.CrawlerDataType.valueOf("interaction"))
                        .releaseTime(releaseTimeToLong)
                        .url(requestUrl)
                        .requestLabelTag(CrawlerEnum.CrawlerRequestType.filter)
                        .requestLabelTag(CrawlerEnum.CrawlerRequestType.result)
                        .addContentKV(Field_I_Likes, likes)
                        .addContentKV(Field_I_Dislikes, noLikes)
                        .build();
                crawlerDataList.add(crawlerDataInteraction);
            } catch (ParseException e) {
                log.error(e.getMessage());
            }
        }
        return crawlerDataList;
    }

    @Override
    public void afterExecute(CrawlerRecordContext crawlerRecordContext) {

    }

    private void requestAgainCrawlerRecord(List<CrawlerRequestRecord> crawlerRequestRecords, CrawlerRequestRecord crawlerRecord) {
        CrawlerBusinessTags crawlerBusinessTags = crawlerRecord.tagsCreator().bizTags();
        int count = 0;
        if (crawlerBusinessTags.hasKVTag(REQUEST_AGAIN_TAG)) {
            count = (int) crawlerBusinessTags.getCategoryTag().getKVTag(REQUEST_AGAIN_TAG).getVal();
            if (count >= 5) {
                log.error(DOMAIN + " download page the number of retries exceeds the limit" +
                        ",request url {}", crawlerRecord.getHttpRequest().getUrl());
                return;
            }
        }

        String requestUrl = crawlerRecord.getHttpRequest().getUrl();
        LabelTag type = crawlerRecord.tagsCreator().requestTags().getCategoryTag().getLabelTags().get("turn_page_item_request");
        CrawlerRequestRecord crawlerRequestRecord = null;
        if (type == null) {
            crawlerRequestRecord = CrawlerRequestRecord.builder()
                    .turnPageRequest(crawlerRecord)
                    .httpUrl(requestUrl)
                    .releaseTime(crawlerRecord.getReleaseTime())
                    .recordKey(crawlerRecord.getRecordKey() + count)
                    .notFilterRecord()
                    .copyBizTags()
                    .copyResultTags()
                    .build();
        } else {
            crawlerRequestRecord = CrawlerRequestRecord.builder()
                    .itemPageRequest(crawlerRecord)
                    .httpUrl(requestUrl)
                    .releaseTime(crawlerRecord.getReleaseTime())
                    .recordKey(crawlerRecord.getRecordKey() + count)
                    .copyBizTags()
                    .copyResultTags()
                    .build();
        }

        if (crawlerRequestRecord == null) {
            return;
        }

        crawlerRequestRecord.getHttpRequest().setExtras(crawlerRecord.getHttpRequest().getExtras());
        crawlerRequestRecord.getHttpRequest().setHeaders(crawlerRecord.getHttpRequest().getHeaders());
        crawlerRequestRecord.setNeedWashPage(crawlerRecord.isNeedWashPage());
        crawlerRequestRecord.setNeedParsedPage(crawlerRecord.isNeedParsedPage());
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV(REQUEST_AGAIN_TAG, ++count);
        crawlerRequestRecords.add(crawlerRequestRecord);
    }

    private void initKeyword(CrawlerRequestRecord requestRecord, CrawlerRequestRecord supportSourceRecord, List<CrawlerRecord> crawlerRecords) {
        HttpPage internalDownloadPage = supportSourceRecord.getInternalDownloadPage();
        Map<String, Object> extras = requestRecord.getHttpRequest().getExtras();
        String searchSourceUrl = (String) extras.get(SEARCH_KW_SOURCE_URL);
        if (StringUtils.isBlank(searchSourceUrl)) {
            log.error("search kw source url can not null!");
            return;
        }
        Json json = internalDownloadPage.getJson();
        String msg = json.jsonPath($_type + ".msg").get();
        if (!"success".equals(msg)) {
            log.error("keyword page [{}] download error!", internalDownloadPage.getRequest().getUrl());
            return;
        }

        List<String> all = json.jsonPath($_type + ".content").all();
        for (String data : all) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            String keyword = jsonObject.getString("keyword");

            try {
                String url = String.format(searchSourceUrl, URLEncoder.encode(keyword, "UTF-8"));
                CrawlerRequestRecord turnRecord = CrawlerRequestRecord.builder()
                        .turnPageRequest(requestRecord)
                        .httpUrl(url)
                        .releaseTime(System.currentTimeMillis())
                        .notFilterRecord()
                        .httpHeads(requestRecord.getHttpRequest().getHeaders())
                        .copyBizTags()
                        .copyResultTags()
                        .build();
                turnRecord.tagsCreator().bizTags().addKeywords(keyword);
                crawlerRecords.add(turnRecord);
            } catch (UnsupportedEncodingException e) {
                log.error(e.getMessage());
            }
        }
    }
}
